<template>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="类型名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="类型名称"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item size="mini" label="授权">
        <el-tree
          :data="menuList"
          :props="menuListTreeProps"
          node-key="menuId"
          ref="menuListTree"
          :default-expand-all="true"
          @check="handleCheckChange"
          show-checkbox
        >
        </el-tree>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button :plain="true" type="info" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { menuTypeSave } from "@/api/rulesmenu";
export default {
  data() {
    return {
      title: "",
      visible: false,
      menuList: [],
      department: [],
      menuListTreeProps: {
        label: "menuName",
        children: "children",
      },
      dataForm: {
        typeId: null,
        name: "",
        remark: "",
        menuList: [],
      },
      dataRule: {
        name: [
          { required: true, message: "类型名称不能为空", trigger: "blur" },
        ],
      },
      tempKey: -666666, // 临时key, 用于解决tree半选中状态项不能传给后台接口问题. # 待优化
    };
  },
  methods: {
    // 菜单树设置当前选中节点
    menuListTreeSetCurrentNode(menuID) {
      // console.log(menuID,'menuID');
      menuID.forEach(t=>{
        this.$refs.menuListTree.setChecked(t,true);
      })
    },
    handleCheckChange(checkedNodes,checkedKeys ) {
      // console.log(checkedNodes,checkedKeys);
      if (checkedKeys.checkedNodes && checkedKeys.checkedNodes.length) {  
        this.dataForm.menuList = []
        checkedKeys.checkedNodes.forEach(data => {        
          // 勾选添加
          let w = {
            creUserId: data.creUserId,
            creTime:data.creTime,
            isEffect: data.isEffect,
            remark: data.remark,
            lastUpdateId: data.lastUpdateId,
            lastUpdateTime: data.lastUpdateTime,
            menuId: data.menuId,
            menuName: data.menuName,
            parentId: data.parentId,
            type: data.type,
            orderNum: data.orderNum,
            groupIdentifys: data.groupIdentifys,
          };
          this.dataForm.menuList.push(w);
        }); 
        // console.log(this.dataForm.menuList,'this.dataForm.menuList');
      } 
    },
    // 表单提交
    dataFormSubmit() {
      // console.log(this.dataForm.menuList);
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          let param = {
            typeId: this.dataForm.typeId,
            name: this.dataForm.name,
            remark: this.dataForm.remark,
            menuList: JSON.stringify(this.dataForm.menuList),
          };
          menuTypeSave(param).then(({ data }) => {
            if (data && data.resultCode === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
